package xhwl.hmo.mall.ui;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.Constant;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.FindAdapter;
import xhwl.hmo.mall.event.ThumpupEvent;
import xhwl.hmo.mall.http.api.FindApi;
import xhwl.hmo.mall.http.api.PageViewApi;
import xhwl.hmo.mall.http.api.ThumbsUpApi;
import xhwl.hmo.mall.http.bean.resp.FindResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.utils.AppUtil;
import xhwl.hmo.mall.utils.SpUtil;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

import static com.umeng.socialize.utils.DeviceConfig.context;

public class FindListActivity extends BaseActivity {
    ImageView img_findlist;
    int name;
    ListView lv_findlist;
    List<FindResp> findRespList = new ArrayList();
    FindAdapter findAdapter;
    RefreshLayout refreshLayout;
    Drawable drawable;
    int page_num=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_list);


    }

    @Override
    public void initViews() {
        refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout_findlist);
        initTitleViews();
        lv_findlist = (ListView) findViewById(R.id.lv_findlist);
        img_findlist = (ImageView) findViewById(R.id.img_findlist);

        if (name == 0) {
            img_findlist.setImageDrawable(getResources().getDrawable(R.drawable.clinical_interpretation_del));
        } else if (name == 1) {
            img_findlist.setImageDrawable(getResources().getDrawable(R.drawable.health_exposition_del));
        } else if (name == 2) {
            img_findlist.setImageDrawable(getResources().getDrawable(R.drawable.product_triall_del));
        }
        findAdapter = new FindAdapter(mContext, findRespList);
        lv_findlist.setAdapter(findAdapter);
        findAdapter.setOnAddClickListener(onItemActionClick);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                findlist(page_num + 1);
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                findlist(1);
            }
        });

    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        name = getIntent().getIntExtra("name", 0);
        titleTextView.setText("HMO发现");




    }

    @Override
    public void initListener() {

        lv_findlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                PageViewApi pageViewapi = new PageViewApi(findRespList.get(position).RECORD_ID);
                HttpManager.getInstance().dealHttp(FindListActivity.this, pageViewapi, new OnNextListener<HttpResp>() {
                    @Override
                    public void onNext(HttpResp httpResp) {
                        if (httpResp.isOk()) {
                            String url= Constant.ARTICAL_URL+findRespList.get(position).RECORD_ID;
                            AppUtil.startWebViewActivity(mContext,url,"");
                        }
                    }
                });

            }
        });
    }

    @Override
    public void loadData() {
        findlist(1);

    }

    FindAdapter.OnAddClickListener onItemActionClick = new FindAdapter.OnAddClickListener() {

        @Override
        public void onItemClick(final int position) {
            ThumbsUpApi thumbsUpApi = new ThumbsUpApi(SpUtil.getString(SpUtil.MEMBER_MOBILE, ""), findRespList.get(position).RECORD_ID);
            HttpManager.getInstance().dealHttp(FindListActivity.this, thumbsUpApi, new OnNextListener<HttpResp>() {
                @Override
                public void onNext(HttpResp httpResp) {
                    if (httpResp.isOk()) {
                        FindResp bean = findRespList.get(position);
                        int thumbs = Integer.valueOf(bean.THUMBS_UP).intValue();
                        if("1".equals(bean.THUMBS_FLAG)){
                            //当前已点赞
                            bean.THUMBS_FLAG="0";
                            thumbs--;
                        }
                        else
                        {
                            bean.THUMBS_FLAG="1";
                            thumbs++;
                        }
                        bean.THUMBS_UP=thumbs+"";
                        findAdapter.notifyDataSetChanged();
                        EventBus.getDefault().post(new ThumpupEvent());
                    } else {
                        ToastUtil.showToast(context, httpResp.message);
                    }

                }
            });
        }
    };
    public void findlist(final int page){
        FindApi findApi = new FindApi(String.valueOf(name), SpUtil.getString(SpUtil.MEMBER_MOBILE, ""),page);
        HttpManager.getInstance().dealHttp(FindListActivity.this, findApi, new OnNextListener<HttpResp<ArrayList<FindResp>>>() {

            @Override
            public void onNext(HttpResp<ArrayList<FindResp>> arrayListHttpResp) {
                if (page > 1) {
                    refreshLayout.finishLoadmore();
                }else
                {
                    refreshLayout.finishRefresh();
                }



                if (arrayListHttpResp.data.size() > 0) {
                    page_num = page;
                    if(page==1)
                    {
                        findRespList.clear();
                    }
                    findRespList.addAll(arrayListHttpResp.data);
                    findAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(FindListActivity.this, getString(R.string.no_more_data));
//                        refreshLayout.setEnableLoadmore(false);
                }


            }
        });

    }
}
